package com.bjpowernode.boss;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bjpowernode.boss.dao.entity.DicPayType;
import com.bjpowernode.boss.dao.mapper.DicPayTypeMapper;
import com.bjpowernode.boss.dao.mapper.DicPayWayMapper;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.dao.result.DicPayTypeGroup;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@MapperScan("com.bjpowernode.boss.dao.mapper")
@SpringBootApplication
public class PowerBossApplication implements CommandLineRunner {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    public static void main(String[] args) {
        SpringApplication.run(PowerBossApplication.class, args);
    }

    //支付方式字典List
    public static List<DicEntity> PAYWAY_DIC_LIST = new ArrayList<>();
    //支付方式的Map( payWayCode, payWayName )
    public static Map<String,String> PAYWAY_NAME_MAP = new HashMap<>();


    //支付类型Map<wayCode,List<DicEntity>
    public static Map<String,List<DicEntity>> PAYTYPE_DIC_MAP = new HashMap<>();
    //支付类型的名称的map
    public static Map<String,String> PAYTYPE_NAME_MAP = new HashMap<>();


    //注入spring容器对象
    @Resource
    private ApplicationContext applicationContext;
    /**
     * CommandLineRunner : SpringBoot中的接口， 就一个run方法
     * 执行run()时间:在spring容器初始化完成后，在业务方法执行之前。
     * run()适合做什么： 项目初始化操作，读取数据库，读取配置文件，生成默认数据。 做一些项目的初始化工作
     */
    @Override
    public void run(String... args) throws Exception {
        //读取字典表的数据， dic_pay_way ,换存到内存对象
        DicPayWayMapper payWayMapper = applicationContext.getBean(DicPayWayMapper.class);
        PAYWAY_DIC_LIST = payWayMapper.selectPayWayDicList();
        //List-->Map集合
        PAYWAY_NAME_MAP = PAYWAY_DIC_LIST.stream().collect(Collectors.toMap(DicEntity::getCode, DicEntity::getLabel));

        //支付类型Map数据
        DicPayTypeMapper payTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        List<DicPayTypeGroup> groupList = payTypeMapper.selectPayTypeGroupContent();

        //Map<String,List<DicEntity>>
        //ALIPAY F2F_PAY#条码支付;HUA_BEI_FEN_QI_PAY#花呗分期
        groupList.forEach(  typeGroup ->{
            List<DicEntity>  dicList = new ArrayList<>();
            //ALIPAY
            String payWayCode = typeGroup.getPayWayCode();

            String typeCode = "";
            String typeName = "";
            //F2F_PAY#条码支付;HUA_BEI_FEN_QI_PAY#花呗分期
            String content = typeGroup.getContent();
            for (String cet : content.split(";")) {
                typeCode = cet.split("#")[0];
                typeName = cet.split("#")[1];
                dicList.add(new DicEntity(typeName,typeCode));
                PAYTYPE_NAME_MAP.put(payWayCode+":"+typeCode,typeName);
            }
            PAYTYPE_DIC_MAP.put(payWayCode,dicList);
        });

        System.out.println("PAYTYPE_DIC_MAP = " + PAYTYPE_DIC_MAP);
    }
}
